{% extends "openlavaweb/base.html" %}
{% block content %}
<div id="failMessageBody"></div>
<script>
	function updateUserInfo(user) {
		$("#totalJobSlots").text( user.total_jobs + " (" + user.total_slots + ")");
		$("#pendingJobSlots").text(user.num_pending_jobs + " (" + user.num_pending_slots + ")");
		$("#runningJobSlots").text(user.num_running_jobs + " (" + user.num_running_slots + ")");
		$("#suspendedJobSlots").text(user.num_suspended_jobs + " (" + user.num_suspended_jobs + ")");
		if (user.cluster_type == "openlava"){
			$("#userSuspendedJobSlots").text(user.num_user_suspended_jobs + " (" + user.num_user_suspended_slots + ")");
			$("#systemSuspendedJobSlots").text(user.num_system_suspended_jobs + " (" + user.num_system_suspended_slots + ")");
		}
				
		var newJobs = [];
            var html = "";
            jQuery.each(user.jobs, function(index, job){
                if (index > 20){
                    return;
                }
                var js = 'jl_' + job.job_id + '_' + job.array_index + '_';
                if ($(js + "_row").html()){
                    html += "<tr>" + $(sel + "_row").html() + "</tr>";
                }else {
                    html += '<tr id="' + js + 'row" class="active">';
                    html += '<td><a href="' + job.url + '">' + job.job_id;
                    if (job.array_index > 0) {
                        html += "[" + job.array_index + "]";
                    }
                    html += '</a></td>';
                    html += '<td><a href="' + job.user_url + '">' + job.user_name + "</a></td>";
                    html += '<td id="' + js + 'status">' + job.status['friendly'] + "</td>";
                    html += '<td id="' + js + 'submit_time">Loading...</td>';
                    html += '<td id="' + js + 'start_time">Loading...</td>';
                    html += '<td id="' + js + 'end_time">Loading...</td';
                    html += "</tr>";
                    newJobs.push([job.job_id, job.array_index]);
                }
            });

            if (html=="") {
                html="<tr><td colspan='6'>No Jobs</td></tr>";
            }

            $("#jobs").html(html);
            jQuery.each(newJobs, function(index, j){
                updateJob(j[0], j[1]);
            });
	}
	function update(){
        olwclient.User.getUser('{{ user.name }}', function(user){
            updateUserInfo(user);
            setTimeout(update, 30000);
        }, function(errType, message){
            $("#failMessageBody").html('<div id="failMessage" class="fade in alert alert-danger">Error! Unable to load user information: ' + message + '</div>');
            $("#failMessage").alert();
            $("#statusMessageBody").empty();
            setTimeout(function(){
				$("#failMessage").alert('close');
			},15000);
            setTimeout(update, 30000);
        });
	}
    $(function(){
        setTimeout(update, 30000);
    });
</script>

<h1>User: {{ oluser.name }}</h1>

<dl class="dl-horizontal">
	<dt>User Name</dt><dd>{{ oluser.name }}</dd>
	<dt>Per Processor Slot Limit</dt><dd>{% if oluser.cluster_type == "openlava" and oluser.max_jobs_per_processor == 2147483648.0 %}Unlimited{% else %}{{ oluser.max_jobs_per_processor }}{% endif %}</dd>
	<dt>Max Jobs</dt><dd>{% if oluser.cluster_type == "openlava" and oluser.max_jobs == 2147483647 %}Unlimited{% else %}{{ oluser.max_jobs }}{% endif %}</dd>
	<dt>Max Slots</dt><dd>{% if oluser.cluster_type == "openlava" and oluser.max_slots == 2147483647 %}Unlimited{% else %}{{ oluser.max_slots }}{% endif %}</dd>
</dl>
<h1>Utilization</h1>
<table class="table table-striped table-condensed">
	<thead>
		<tr>
			<th>Total Num Jobs (Slots)</th>
			<th>Num Pending Jobs (Slots)</th>
			<th>Num Running Jobs (Slots)</th>
			<th>Num Suspended Jobs (Slots)</th>
			{% if user.cluster_type == "openlava" %}
				<th>Num User Suspended Jobs (Slots)</th>
				<th>Num System Suspended Jobs (Slots)</th>
			{% endif %}
			<th>Num Reserved Slots</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td id="totalJobSlots">{{ oluser.total_jobs }} ({{ oluser.total_slots }})</td>
			<td id="pendingJobSlots">{{ oluser.num_pending_jobs }} ({{oluser.num_pending_slots}})</td>
			<td id="runningJobSlots">{{ oluser.num_running_jobs }} ({{ oluser.num_running_slots }})</td>
			<td id="suspendedJobSlots">{{ oluser.num_suspended_jobs }} ({{ oluser.num_suspended_jobs }})</td>
			{% if user.cluster_type == "openlava" %}
				<td id="userSuspendedJobSlots">{{ oluser.num_user_suspended_jobs }} ({{ oluser.num_user_suspended_slots }})</td>
				<td id="systemSuspendedJobSlots">{{ oluser.num_system_suspended_jobs }} ({{ oluser.num_system_suspended_slots }})</td>
			{% endif %}
			<td id="reservedSlots">{{ oluser.num_reserved_slots }}</td>
		</tr>
	</tbody>
</table>
<h1>Jobs</h1>
{% include "openlavaweb/widgets/job_list.html" with job_list=oluser.jobs job_limit=20 %}
<a href="{% url "olw_job_list" %}?user_name={{ oluser.name|urlencode }}">View All Jobs</a>

{% endblock %}

